package scanpackage.listener;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
@Component
public class CustomJmsListener  {
    @Autowired
    private CacheManager cacheManager;
    private static final String ORDER_RESPONSE_QUEUE = "fallback";
    //收到信息时的动作
    @JmsListener(destination=ORDER_RESPONSE_QUEUE)
    public void onMessage(Message m)  {
        try {
            ObjectMessage message = (ObjectMessage) m;
            String reuslt=message.getObject().toString().replace("\"","");
            String[] reusltstrs=reuslt.split("_");
            Cache cache = cacheManager.getCache("cacheTest");
            final Element putGreeting = new Element(reusltstrs[0], reusltstrs[1]);
            cache.put(putGreeting);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
